package com.gk.panda.commons.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Map;

public class UserUtil {

    private static final String secret = "pandapark";

    public static String getCurrentUser() {
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String username = null;
        try {
            username = URLDecoder.decode(request.getHeader("username"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return username;
//        Map<String, Object> map = getUserInfoMap();
//        return String.valueOf(map.get("username"));
    }

    public static int getCurrentUserOrgId() {
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        return Integer.parseInt(request.getHeader("orgId"));
//        Map<String, Object> map = getUserInfoMap();
//        return Integer.parseInt(String.valueOf(map.get("orgId")));
    }

    public static String getCurrentUserOrgName() throws UnsupportedEncodingException {
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String orgName = request.getHeader("orgName");
        return URLDecoder.decode(orgName,"utf-8");
//        Map<String, Object> map = getUserInfoMap();
//        return String.valueOf(map.get("orgName"));
    }
    public static String getCurrentUserOrgAbbr() {
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        return request.getHeader("orgAbbr");
//        Map<String, Object> map = getUserInfoMap();
//        return String.valueOf(map.get("orgAbbr"));
    }
    public static String getCurrentUserName() throws UnsupportedEncodingException {
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String name = request.getHeader("name");
        return URLDecoder.decode(name,"utf-8");
//        Map<String,Object> map = getUserInfoMap();
//        return String.valueOf(map.get("name"));
    }

    public static Map<String, Object> getUserInfoMap() {
        HttpServletRequest request =
                ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
        String token = request.getHeader("authorization");
        return parseToken(token);
    }

    public static Map<String,Object> parseToken(String token){
        Jws<Claims> claimsJws = Jwts.parser().setSigningKey(secret).parseClaimsJws(token);
        return claimsJws.getBody();
    }

}
